//
//  YSNavBar.swift
//  niuguwang
//
//  Created by BrightLi on 2021/3/29.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import Foundation
import UIKit
import SDWebImage

public let NavTitleRect = CGRect(x:ScreenWidth*0.25,y:0,width:ScreenWidth*0.5,height:NavBarHeight)
public let NavButtonRect = CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight)

public let nav_TitleColor: UInt32 = STheme.H_D1

@objc public enum SNavIconType: Int
{
    case left
    case right
}

@objcMembers public class SNavIcon:UIView
{
    public let icon:UIImageView = UIImageView()
    
    lazy var title:UILabel={
        let result=UILabel()
        result.font=SFitUtil.fitFont(14)
        return result
    }()
    
    let hotView:UIView! = SThemeUtil.hotView()
    public var type:SNavIconType = .left
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override init(frame:CGRect)
    {
        super.init(frame: frame)
        self.addSubview(icon)
        self.addSubview(title)
        self.addSubview(hotView)
    }
    
    dynamic public func textWith(_ text:String,color:UIColor,onClick:@escaping (() -> ()))
    {
        title.text=text
        title.textColor=color
        let size=SFitUtil.sizeOf(title)
        if type == .left{
            self.title.textAlignment = .left
            self.title.frame = CGRect(x:NavBarItemMargin,y:(CGFloat(NavBarHeight)-size.height)/2,width:size.width,height:size.height)
        }else{
            self.title.textAlignment = .right
            self.title.frame = CGRect(x:self.bounds.width-size.width-NavBarItemMargin,y:(CGFloat(NavBarHeight)-size.height)/2,width:size.width,height:size.height)
        }
        self.title.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
    }
    
    dynamic public func iconOf(_ icon:String,onClick:@escaping (() -> ()))
    {
        if let image = UIImage(named:icon){
            self.icon.image = image
            if type == .left{
                self.icon.frame = CGRect(x:NavBarItemMargin,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
            }else{
                self.icon.frame = CGRect(x:self.bounds.width-image.size.width-NavBarItemMargin,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
            }
            self.s_clickBlock { (tap:UITapGestureRecognizer) in
                onClick()
            }
        }
    }
    
    dynamic public func changeIcon(_ icon:String){
        if let image = UIImage(named:icon){
            self.icon.image = image
            if type == .left{
                self.icon.frame = CGRect(x:NavBarItemMargin,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
            }else{
                self.icon.frame = CGRect(x:self.bounds.width-image.size.width-NavBarItemMargin,y:(CGFloat(NavBarHeight)-image.size.height)/2,width:image.size.width,height:image.size.height)
            }
        }
    }
    
    dynamic public func headOf(_ url:String,placeHolder:String,size:CGFloat,onClick:@escaping (() -> ()))
    {
        icon.layer.masksToBounds = true;
        icon.layer.cornerRadius = size/2;
        icon.frame = CGRect(x:SFitUtil.fit(65-29)*0.5,y:8,width:size,height:size)
        hotView.frame = CGRect(x:SFitUtil.fit(65-29)*0.5 - SFitUtil.fit(5),y:8 - 5,width:size + 10,height:size + 10)
        SUIUtil.setImage(icon,url:url,placeholder:placeHolder)
        self.hotView.s_clickBlock { (tap:UITapGestureRecognizer) in
            onClick()
        }
    }
    
    dynamic public func headUpdateWith(_ url:String,placeHolder:String)
    {
        SUIUtil.setImage(icon, url: url, placeholder: placeHolder)
    }
}

@objcMembers open class YYNavTitle:UIView
{
    public lazy var title:UILabel = {
        let result = SThemeUtil.boldLabel("", color: nav_TitleColor, fontSize:16)
        result.textAlignment = .center
        result.adjustsFontSizeToFitWidth = true
        result.minimumScaleFactor = 0.8
        return result
    }()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(title)
        title.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

@objcMembers open class YYNavWithSubTitle:UIView
{
    public lazy var title:UILabel = {
        let result = SThemeUtil.boldLabel("", color: nav_TitleColor, fontSize:17)
        result.textAlignment = .center
        result.adjustsFontSizeToFitWidth = true
        result.minimumScaleFactor = 0.8
        return result
    }()
    
    public lazy var subTitle:UILabel = {
         let result = SThemeUtil.label("", color: nav_TitleColor, fontSize:12)
         result.textAlignment = .center
         result.adjustsFontSizeToFitWidth = true
         result.minimumScaleFactor = 0.8
         return result
     }()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(title)
        addSubview(subTitle)
        title.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(SFitUtil.fit(1))
        }
        
        subTitle.snp.makeConstraints { (make) in
             make.left.right.equalTo(0)
            make.top.equalTo(title.snp.bottom).offset(0)
        }
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }    
    public override  var intrinsicContentSize: CGSize {
        return CGSize( width:ScreenWidth*0.5,height:NavBarHeight)
    }
}

@objcMembers open class YYNavBase:UIView
{
    open lazy var leftIcon :SNavIcon = {
        let leftIcon:SNavIcon=SNavIcon.init(frame: CGRect(x:0,y:0,width:NavButtonWidth,height:NavBarHeight))
        leftIcon.type = .left
        return leftIcon
    }()
    
    open lazy var rightIcon :SNavIcon = {
        let rightIcon:SNavIcon=SNavIcon.init(frame: CGRect(x:ScreenWidth-NavButtonWidth,y:0,width:NavButtonWidth,height:NavBarHeight))
        rightIcon.type = .right
        return rightIcon
    }()
    
    var titleView:UIView?
}

@objcMembers public class YSNavBar:YYNavBase
{
    open lazy var navTitle:YYNavTitle = {
        return YYNavTitle.init(frame: NavTitleRect)
    }()
    
    open lazy var line:UIView = {
        let result = SThemeUtil.viewOfColor(SUIUtil.colorOf(0xeeeeee))
        return result
    }()
    
    public var bottomLine:CGFloat = 0
    
    open var title:String{
        get{
            if let navTitle = titleView as? YYNavTitle{
                return navTitle.title.text ?? ""
            }
            return ""
        }
        set{
            if let navTitle = titleView as? YYNavTitle{
                navTitle.title.text = newValue
            }
        }
    }
    
    open var titleColor:UIColor{
        get{
            if let navTitle = titleView as? YYNavTitle{
                return navTitle.title.textColor
            }
            return UIColor.clear
        }
        set{
            if let navTitle = titleView as? YYNavTitle{
                navTitle.title.textColor = newValue
            }
        }
    }
    
    open lazy var barView:UIView = {
        let barView = UIView(frame:CGRect(x:0,y:StatusBarHeight,width:ScreenWidth,height:NavBarHeight))
        barView.addSubview(leftIcon)
        if let title = titleView{
            barView.addSubview(title)
        }
        barView.addSubview(rightIcon)
        if bottomLine>0{
            barView.addSubview(line)
            line.snp.makeConstraints({ (make) in
                make.left.right.equalTo(0)
                make.bottom.equalTo(0)
                make.height.equalTo(0.5)
            })
        }
        return barView
    }()
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame:CGRect)
    {
        super.init(frame: frame)
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        setupUI()
    }
    
    dynamic func setupUI()
    {
        if barView.superview != nil{
            return
        }
        self.addSubview(barView)
    }
        
    dynamic public class func initWith(_ color:UInt32,titleColor:UInt32,title:String) -> YSNavBar
    {
        let navBar = YSNavBar.init(frame: NavRect)
        navBar.backgroundColor = SUIUtil.colorOf(color)
        let titleView:YYNavTitle = YYNavTitle.init(frame: NavTitleRect)
        titleView.title.textColor = SUIUtil.colorOf(titleColor)
        titleView.title.text = title
        navBar.titleView = titleView
        return navBar;
    }
    
    dynamic public class func initWith(_ titleColor:UInt32,title:String) -> YSNavBar
    {
        let navBar = YSNavBar.init(frame: NavRect)
        navBar.backgroundColor = UIColor.clear
        let titleView:YYNavTitle = YYNavTitle.init(frame: NavTitleRect)
        titleView.title.textColor = SUIUtil.colorOf(titleColor)
        titleView.title.text = title
        navBar.titleView = titleView
        return navBar;
    }
    
    dynamic public class func initWith(_ color:UInt32,titleView:UIView) -> YSNavBar
    {
        let navBar = YSNavBar.init(frame: NavRect)
        navBar.backgroundColor = SUIUtil.colorOf(color)
        navBar.titleView = titleView
        navBar.titleView?.frame = NavTitleRect
        
        return navBar;
    }
}
